<?php

function isLogin()
{
	$arr['user_Id'] = session('user_id');
	if(session('openid') == '' && session('user_id')){		
		to();
	}elseif(session('user_id') == '' && session('openid')){
		$user = M('user') -> where(array('openid' => session('openid'))) -> getField('user_Id');
		if($user){
			session('user_id',$user);
		}else{
			return 1;
		}
	}elseif(session('user_id') == '' && session('openid') == ''){		
		return 1;
	}else{
		num($arr['user_Id']);
    	usePoints($arr['user_Id']);
	}

}


//计算消费积分
function points($userid)
{
	$logModel = M('log');
	$userModel = M('user');

	$arr['user_Id'] = $userid;
	$arr['reason'] = '消费积分';
	$res = $logModel->where($arr)->select();

	//定义一个变量，用于保存积分
	$log['pay_points'] = 0;
	for ($i=0; $i < count($res); $i++) { 
		$log['pay_points'] = $res[$i]['points'] + $log['pay_points'];
	}

	//计算有几个积分单元
	$configModer = M('config');
	$configRes   = $configModer->select();
    $weight = $configRes[0]['weight'];
	
	$log['unit'] = floor($log['pay_points']/$weight);
	if($log['unit'] < 0)
	{
		$log['unit'] = 0;
	}

	$Model = new \Think\Model();
	$new_point = $Model->execute("update user set pay_points=".$log['pay_points'].", unit =".$log['unit']." where user_Id = ".$arr['user_Id']);

}

/*计算返还积分*/
function retPoints($userid)
{
	$logModel = M('log');
	$userModel = M('user');

	$arr['user_Id'] = $userid;
	$resUser = $userModel->where($arr)->select();
	$arr['reason'] = '赠送积分';
	$res = $logModel->where($arr)->select();

	//定义一个变量，用于保存积分
	$log['return_points'] = 0;
	for ($i=0; $i < count($res); $i++) { 
		$log['return_points'] = $res[$i]['points'] + $log['return_points'];
	}
	/*计算weight里面的返还积分*/
	$wModel = M('weight');
	$w['user_id'] = $userid;
	$w['is_user'] = 1;
	$wRes = $wModel->where($w)->select();
	for ($j=0; $j < count($wRes); $j++) { 
		$log['return_points'] = $wRes[$j]['back'] + $log['return_points'];
	}
	
	/*计算weight里面的返还积分end*/

	$Model = new \Think\Model();
	$new_point = $Model->execute("update user set return_points= ".$log['return_points']." where user_Id = ".$arr['user_Id']);
	
}

/*计算可提现积分*/
function usePoints($userid)
{
	retPoints($userid);
	$payModel = M('pay');
	$userModel = M('user');

	//查询会员信息
	$arrU['user_Id'] = $userid;
	$resUser = $userModel->where($arrU)->select();
	
	/*会员提现通过的记录*/
  	$arr['user_id'] = $userid;
    $arr['is_user'] = 1;
    $arr['apply'] = 1;
    $arr['isApply'] = 1;
    
    $payRes = $payModel->where($arr)->select();

    $res = array();
    for ($i=0; $i < count($payRes); $i++) { 
        //判断此记录是不是提现记录
        $resMoney = strstr($payRes[$i]['money'], '-');
        if($resMoney)
        {
			array_push($res,$payRes[$i]);
            //$res[$i] = $payRes[$i];
        }
    }
	$pay['use_points'] = $resUser[0]['return_points'];
	for ($i=0; $i < count($res); $i++) { 
		$pay['use_points'] = $res[$i]['money'] + $pay['use_points'];
	}
	/*会员提现记录end*/
	/*会员提现申请中的的记录*/
  	$arr1['user_id'] = $userid;
    $arr1['is_user'] = 1;
    $arr1['apply'] = 0;
    $arr1['isApply'] = 0;
    
    $payRes1 = $payModel->where($arr1)->select();

    $res1 = array();
    for ($i=0; $i < count($payRes1); $i++) { 
        //判断此记录是不是提现记录
        $resMoney1 = strstr($payRes1[$i]['money'], '-');
        if($resMoney1)
        {
			array_push($res1,$payRes1[$i]);
            //$res1[$i] = $payRes1[$i];
        }
    }
	
	
	for ($i=0; $i < count($res1); $i++) { 
		$pay['use_points'] = $res1[$i]['money'] + $pay['use_points'];
	}	
	
	/*会员提现记录end*/
	$Model = new \Think\Model();
	$new_point = $Model->execute("update user set use_points = '".$pay['use_points']."' where user_Id = ".$arr1['user_id']);
}

//计算可返积分单元
function num($id)
{
	points($id);
	$arr['user_id'] = $id;
	$arr['is_user'] = 1;
	$weightModel = M('weight');
    $weightRes = $weightModel->where($arr)->select();
    $back = 0;
    for ($i=0; $i < count($weightRes); $i++) { 
		$back = $weightRes[$i]['back'] + $back;
	}
	$configModer = M('config');
	$configRes   = $configModer->select();
    $weight = $configRes[0]['weight'];
    $unit = floor($back/$weight);
	if($unit < 0)
	{
		$unit = 0;
	}

	$userModel = M('user');
	$user['user_Id'] = $id;
	$uRes = $userModel->where($user)->select();

	$user['isunit'] = $uRes[0]['unit'] - $unit;
	$Model = new \Think\Model();
	$new_point = $Model->execute("update user set isunit =".$user['isunit']." where user_id = ".$user['user_Id']);
}


function to()
{
	$mysql_server_name='127.0.0.1'; //改成自己的mysql数据库服务器
 
	$mysql_username='root'; //改成自己的mysql数据库用户名
	 
	$mysql_password='cdzx123321'; //改成自己的mysql数据库密码
	 
	$mysql_database='cdzx'; //改成自己的mysql数据库名



	$conn=mysql_connect($mysql_server_name,$mysql_username,$mysql_password) or die("数据库链接错误") ; //连接数据库
	 
	mysql_query("set names 'utf8'"); //数据库输出编码 
	 
	mysql_select_db($mysql_database); //打开数据库
	 
	$sql ="select * from app "; //SQL语句
	 
	$result = mysql_query($sql,$conn); //查询
	$row = mysql_fetch_array($result);

	$appId = $row['appId'];
    
	$encrypt = urlencode('http://cdzx.laobing2016.com/index.php/Admin/Login/index');
	$url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$appId.'&redirect_uri='.$encrypt.'&response_type=code&scope=snsapi_base&state=123#wechat_redirect'; 
	header('Location:'.$url);
}


/*//获取返还积分
function returnPoints()
{
	//获取返还权重
	$configModer = M('config');
	$configRes   = $configModer->select();
    $cWeight = $configRes[0]['weight'];

	$weight['user_id'] = session('user_id');
	$weightModer = M('weight');
	$weightRes       = $weightModer->where($weight)->select();

	$return_points = 0;
	for ($i=0; $i < count($weightRes); $i++) { 
		$return_points = $return_points + $weightRes[$i]['back'];
	}

	$arr['user_Id'] = session('user_id');
	$user['return_points'] = $return_points;
	$UserModer = M('user');
	$userRes    = $UserModer->where( $arr )->select();

	$unit = floor( $user['return_points']/$cWeight );
	$user['isunit'] = $userRes[0]['unit']-$unit;

	$res       = $UserModer->data( $user )->where( $arr )->save();

}*/